package com.link.base.base.visit.controller;

import com.link.base.base.visit.model.Visit;
import com.link.base.base.visit.service.VisitService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.KeyGenerateService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 拜访
 *
 * @author 宁蓝昊
 * @date 2019/03/18 19:59
 */
@Controller
@RequestMapping("/link/visit")
public class VisitController extends BasicController<Visit> {
    private static final Logger logger = LogManager.getLogger(VisitController.class);

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private VisitService visitService;

    @Override
    public BasicService<Visit> getBasicService() throws Exception {
        return visitService;
    }

    /**
     * 保存拜访记录/申请
     *
     * @param entity
     * @return java.util.Map
     * @author NingLanhao
     * @date 2019-03-20 11:20
     */
    @RequestMapping({"/upsertVisit"})
    @ResponseBody
    public Map<String, Object> upsertVisit(@JsonParam Visit entity, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        HashMap result = new HashMap();
        try {
            Visit visit = visitService.upsertVisit(entity);
            result.put("success", true);
            result.put("newRow", visit);
        } catch (BasicServiceException e) {
            logger.error("BasicController捕获异常: ", e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            logger.error("BasicController捕获异常: ", e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
